const app = getApp()
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog';
var bmap = require('../../libs/bmap-wx.min.js');
var wxMarkerData = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    customCalloutid:0,
    customCalloutobj:{
      title:'',
      state:'',
      date:''
    },
    markers: [{
        "name": "基站A",
        "latitude": 31.266447251086,
        "longitude": 120.632090109,
        "address": "江苏省苏州市吴中区宝带东路399号丽丰购物中心南区B1",
        "province": "江苏省",
        "city": "苏州市",
        "area": "吴中区",
        "street_id": "8a5d71dbd59a755c2fb96d45",
        "telephone": "(0512)67071271",
        "detail": 1,
        "uid": "8a5d71dbd59a755c2fb96d45",
        "width": 40,
        "height": 36,
        "iconPath": "../../static/images/mapimg/placedefault.png",
        "iconTapPath": "../../static/images/mapimg/place.png",
        "id": 0,
        customCallout: {
          anchorY: 0, // Y轴偏移量
          anchorX: 0, // X轴偏移量
          display: 'ALWAYS', // 'BYCLICK':点击显示; 'ALWAYS':常显
        },
      },
      {
        "name": "基站B",
        "latitude": 31.265754862166,
        "longitude": 120.62913207103,
        "address": "新华书店旁东吴北路66号",
        "province": "江苏省",
        "city": "苏州市",
        "area": "吴中区",
        "street_id": "bbee5d30550b40c180a00ce5",
        "telephone": "(0512)62539638",
        "detail": 1,
        "uid": "bbee5d30550b40c180a00ce5",
        "width": 40,
        "height": 36,
        "iconPath": "../../static/images/mapimg/placedefault.png",
        "iconTapPath": "../../static/images/mapimg/place.png",
        "id": 1,
        customCallout: {
          anchorY: 0, // Y轴偏移量
          anchorX: 0, // X轴偏移量
          display: 'ALWAYS', // 'BYCLICK':点击显示; 'ALWAYS':常显
        },
      }
    ],
    latitude: '',
    longitude: '',
    placeData: {}
  },
  makertap: function (e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
    that.changeMarkerColor(wxMarkerData, id);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var BMap = new bmap.BMapWX({
      ak: ''
      // ak: 'c8zaQqrc40gt0WuFHvPoLFES8rTPs1sK'
    });
    var fail = function (data) {
      console.log(data, '1')
    };
    var success = function (data) {
      console.log(data.wxMarkerData);
      wxMarkerData = that.data.markers
      wxMarkerData.map((item, index) => {
        item.width = 40
        item.height = 36
        if (index == 0) {
          console.log(item);
          let newObject = {}
        newObject.title=item.name
        newObject.state=item.detail==1?'正常':'异常'
        newObject.date=item.province
        that.setData({
          customCalloutid:item.id,
          customCalloutobj:newObject
        })
          item.iconPath = "../../static/images/mapimg/place.png";
        } else {
          item.iconPath = "../../static/images/mapimg/placedefault.png";
        }
      })
      // wxMarkerData = data.wxMarkerData
      // wxMarkerData.map((item,index)=>{
      //   item.width = 40
      //   item.height = 36
      //   item.id = index
      // })
      that.setData({
        markers: wxMarkerData
      });
      console.log(wxMarkerData);
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });
    }
    // 发起geocoding检索请求 
    BMap.geocoding({
      address: "苏州国际科技大厦",
      fail: fail,
      success: success,
      iconPath: '../../static/images/mapimg/placedefault.png',
      iconTapPath: '../../static/images/mapimg/place.png'
    });
    // BMap.search({
    //     "query": '美食',
    //     fail: fail,
    //     success: success,
    //     iconPath: '../../static/images/mapimg/placedefault.png',
    //     iconTapPath: '../../static/images/mapimg/place.png'
    // });
  },
  //点击显示信息,调整为新窗口
  //   showSearchInfo: function (data, i) {
  //     wx.showModal({
  //         title: data[i].name,
  //         content: data[i].address,
  //         showCancel: true, //是否显示取消按钮
  //         cancelText: "关闭", //默认是“取消”
  //         cancelColor: '#000', //取消文字的颜色
  //         confirmText: "导航", //默认是“确定”
  //         confirmColor: '#000', //确定文字的颜色
  //         success(res) {
  //             if (res.confirm) {
  //                 console.log('用户点击导航')
  //                 let latitude = parseFloat(data[i].latitude)
  //                 let longitude = parseFloat(data[i].longitude)
  //                 let name = data[i].title
  //                 let address = data[i].address
  //                 wx.openLocation({
  //                     latitude,
  //                     longitude,
  //                     name,
  //                     address,
  //                     scale: 18
  //                 })
  //             } else if (res.cancel) {
  //                 console.log('用户点击关闭')
  //             }
  //         }
  //     })
  // },
  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        address: '地址：' + data[i].address + '\n',
        name: '名称：' + data[i].name
      }
    });
  },
  changeMarkerColor: function (data, id) {
    var that = this;
    var markersTemp = [];
    for (var i = 0; i < data.length; i++) {
      if (i === id) {
        console.log(data[i]);
        let newObject = {}
        newObject.title=data[i].name
        newObject.state=data[i].detail==1?'正常':'异常'
        newObject.date=data[i].province
        that.setData({
          customCalloutid:data[i].id,
          customCalloutobj:newObject
        })
        data[i].iconPath = "../../static/images/mapimg/place.png";
      } else {
        data[i].iconPath = "../../static/images/mapimg/placedefault.png";
      }
      markersTemp[i] = data[i];
    }
    that.setData({
      markers: markersTemp
    });
  },
  someStation(){
    let that = this
    console.log('跳转设备列表待定页面');
    // wx.switchTab({
    //   url: '/pages/powerstation/powerstation?customCalloutobj='+JSON.stringify(that.data.customCalloutobj),
    // })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})